﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using WITM.DAL;

namespace WITM.Util
{
    public class ChartProvider
    {

        public Image GetChart()
        {
            Bitmap bmp = new Bitmap(200, 200);
            Graphics gr = Graphics.FromImage(bmp);
            Brush brush = new SolidBrush(Color.Orange);
            Pen p = new Pen(brush);
            gr.FillRectangle(new SolidBrush(Color.Orchid), gr.VisibleClipBounds);
            p.Color = Color.White;
            gr.DrawLine(p, 10, 10, 100, 100);
            // gr.Dispose();
            return bmp;
        }

        public Image GetChart(int width, int height, List<SpendingSum> sumItems)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics gr = Graphics.FromImage(bmp);
            Pen pen = new Pen(Brushes.Gold);
            gr.DrawRectangle(pen, new Rectangle(0, 0, width - 1, height - 1));
            DrawGrid(gr, pen, sumItems, width - 1, height - 1);
            return bmp;
        }

        private void DrawGrid(Graphics gr, Pen pen, List<SpendingSum> sumItems, int width, int height)
        {
            int columns = sumItems.Count;
            if (columns == 0) return;
            float colWidth = (float)width / columns;
            float heihgt = (float)height;
            
            for (int i = 0; i <= columns; i++)
            {
                float x = i * colWidth;
                gr.DrawLine(pen, x, 0f, x, gr.VisibleClipBounds.Bottom);
            }
            decimal maxAmount = sumItems.Max(s => s.Amount);
            int faktor = GetFactor(maxAmount);
            int rows = (int)(maxAmount / faktor) + 1;
            float rowHeihgt = (float)height / rows;
            for (int i = 0; i < rows; i++)
            {
                float y = i * rowHeihgt;
                gr.DrawLine(pen, 0, y, width, y);
            }
            List<PointF> points = new List<PointF>();
            sumItems.Sort(delegate(SpendingSum s1, SpendingSum s2) { return s1.Date.CompareTo(s2.Date); });
            points.Add(new PointF(0, height));
            for (int i = 0; i < sumItems.Count; i++)
            {
                PointF point = new PointF(i * colWidth + (colWidth / 2), 
                    (float)height - (float)sumItems[i].Amount / faktor * rowHeihgt);
                points.Add(point);
            }
            points.Add(new PointF(width, height));
            // gr.DrawCurve(pen, points.ToArray());
            gr.FillClosedCurve(Brushes.Orchid, points.ToArray());
        }

        private int GetFactor(decimal amount)
        {
            int res = 1;
            while (amount > 100)
            {
                res = res * 10;
                amount = amount / 10;
            }
            return res;
        }

        public Image GetPie(int width, int height, List<SpendingSum> sumItems)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics gr = Graphics.FromImage(bmp);
            Pen pen = new Pen(Brushes.Gold);
            gr.DrawRectangle(pen, new Rectangle(0, 0, width - 1, height - 1));
            DrawPie(gr, pen, sumItems, width - 1, height - 1);
            return bmp;
        }

        private void DrawPie(Graphics gr, Pen pen, List<SpendingSum> sumItems, int width, int height)
        {
            int columns = sumItems.Count;
            if (columns == 0) return;
            int totalPeaces = sumItems.Count;
            int degrees = 360 - totalPeaces - 1;
            decimal startAngle = -90m;
            decimal total = sumItems.Sum(s => s.Amount);
            int pieWidth = height > width ? width : height;
            pen.Color = Color.Aqua;
            Random r = new Random();
            Font f = new Font("Times", 12);
            

            for (int i = 0; i < totalPeaces; i++)
            {
                Color color = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                pen.Color = color;
                decimal angle = sumItems[i].Amount / total * (decimal)degrees;
                gr.FillPie(pen.Brush, new Rectangle(0, 0, pieWidth - 1, pieWidth - 1), (float)startAngle, (float)angle);
                startAngle += angle + 1m;
                gr.FillRectangle(pen.Brush, new Rectangle(pieWidth - 1, 5, 25, 20));
                gr.DrawString("17 %", f, pen.Brush, pieWidth - 1 + 30, 5);
            }
        }
    }
}

